//
//  ZHIBOViewController.swift
//  FirstSwiftProduct
//
//  Created by 范泽华 on 2017/11/2.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit
import YYKit.NSObject_YYModel
import MJRefresh

@objc protocol zhiboDelegate {
    func didSelectIndex(lives:livess)
}

class ZHIBOViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    var tableView = UITableView()
    var dataArr = NSMutableArray()
    let liveModel = HotModel()
    
    weak var delegate : zhiboDelegate?
    
    var header : MJRefreshNormalHeader?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        
        createUI()
        tableView.register(UINib(nibName: "ZHIBOCell", bundle: Bundle.main), forCellReuseIdentifier: "ZHIBOCell")
        
        header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(getData))
        tableView.mj_header = header
        getData()
    }

    func createUI() -> Void {
        
        tableView = UITableView.init(frame: CGRect(x:0, y: 0, width: SCREEN_WIDTH, height: SCRENN_HEIGHT - kTabBarHight - 35 * scaleWidth - (CGFloat)(kStatus_Navigation_height )))
      
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
        
    }

    func getData() -> Void {
        
        DispatchQueue(label: "label", qos: .userInitiated, attributes: .concurrent, autoreleaseFrequency: .workItem, target: nil).async {
            
            self.liveModel.getModelData { (arrar) in
                
                self.tableView.reloadData()
                self.header?.endRefreshing()
            }
        }
        
        
        
//        let model = HotModel()
//        model.getModelData { (dataArr) in
//            self.dataArr.addObjects(from: dataArr as! [Any])
//            self.tableView.reloadData()

//        }
        
//
//        HttpTool.shareTool.requestJsonDataWithUrl(Type: .get, url: kHotLiveAPI, parames: [:], succeed: { (success, result) in
//
////            let dict:NSDictionary = result as! NSDictionary
////            self.dataArr = NSMutableArray.init()
////            self.dataArr.addObjects(from: dict.object(forKey: "lives") as! [Any])
////            let hotModel = HotModel.model(withJSON: result as Any)
////            self.dataArr.addObjects(from: (hotModel?.lives)! as! [Any])
//
////            self.dataArr.add(hotModel?.lives as AnyObject)
//
//            self.tableView.reloadData()
//        }, error: { (error) in
//
//        }) { (fail) in
//
//        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return SCREEN_WIDTH + 65
    }
    
    // UITableViewDelegate,UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.liveModel.lives.count
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ZHIBOCell") as? ZHIBOCell

        cell?.selectionStyle = UITableViewCellSelectionStyle.none
        
        let model:livess = liveModel.lives[indexPath.row]
//        print(indexPath.row,model.creator?.nick as Any,model.creator?.portrait as Any)
//        cell?.textLabel?.text = model.creator?.nick
        cell?.configWithModel(model: model)
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
        //跳转播放界面
       
        
        self.delegate?.didSelectIndex(lives: liveModel.lives[indexPath.row])
        
        
    }
    
}
